home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / packages / add-log.el next >
Encoding:
Text File  |  1995-03-25  |  16.9 KB  |  466 lines

  1. ;;; add-log.el --- change log maintenance commands for Emacs
  2.  
  3. ;; Copyright (C) 1985, 1986, 1988, 1993, 1994 Free Software Foundation, Inc.
  4.  
  5. ;; Keywords: maint
  6.  
  7. ;; This file is part of XEmacs.
  8.  
  9. ;; XEmacs is free software; you can redistribute it and/or modify it
  10. ;; under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 2, or (at your option)
  12. ;; any later version.
  13.  
  14. ;; XEmacs is distributed in the hope that it will be useful, but
  15. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. ;; General Public License for more details.
  18.  
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  21. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  
  23. ;;; Synched up with: FSF 19.28.
  24.  
  25. ;;; Commentary:
  26.  
  27. ;; This facility is documented in the Emacs Manual.
  28.  
  29. ;;; Code:
  30.  
  31. ;;;###autoload
  32. (defvar change-log-default-name nil
  33.   "*Name of a change log file for \\[add-change-log-entry].")
  34.  
  35. ;;;###autoload
  36. (defvar add-log-current-defun-function nil
  37.   "\
  38. *If non-nil, function to guess name of current function from surrounding text.
  39. \\[add-change-log-entry] calls this function (if nil, `add-log-current-defun'
  40. instead) with no arguments.  It returns a string or nil if it cannot guess.")
  41.  
  42. ;;;###autoload
  43. (defvar add-log-full-name nil    ; XEmacs change
  44.   "*Full name of user, for inclusion in ChangeLog daily headers.
  45. This defaults to the value returned by the `user-full-name' function.")
  46.  
  47. ;; So that the dump-time value doesn't go into loaddefs.el with the autoload.
  48. (or add-log-full-name (setq add-log-full-name (user-full-name)))
  49.  
  50. ;;;###autoload
  51. (defvar add-log-mailing-address nil    ; XEmacs change
  52.   "*Electronic mail address of user, for inclusion in ChangeLog daily headers.
  53. This defaults to the value of `user-mail-address'.")
  54.  
  55. ;; So that the dump-time value doesn't go into loaddefs.el with the autoload.
  56. (or add-log-mailing-address
  57.     (setq add-log-mailing-address user-mail-address))
  58.  
  59. (defun change-log-name ()
  60.   (or change-log-default-name
  61.       (if (eq system-type 'vax-vms) 
  62.       "$CHANGE_LOG$.TXT" 
  63.     (if (eq system-type 'ms-dos)
  64.         "changelo"
  65.       "ChangeLog"))))
  66.  
  67. ;;;###autoload
  68. (defun prompt-for-change-log-name ()
  69.   "Prompt for a change log name."
  70.   (let ((default (change-log-name)))
  71.     (expand-file-name
  72.      (read-file-name (format "Log file (default %s): " default)
  73.              nil default))))
  74.  
  75. ;;;###autoload
  76. (defun find-change-log (&optional file-name)
  77.   "Find a change log file for \\[add-change-log-entry] and return the name.
  78. Optional arg FILE-NAME specifies the file to use.
  79. If FILE-NAME is nil, use the value of `change-log-default-name' if non-nil.
  80. Otherwise, search in the current directory and its successive parents
  81. for a file named `ChangeLog' (or whatever we use on this operating system).
  82.  
  83. Once a file is found, `change-log-default-name' is set locally in the
  84. current buffer to the complete file name."
  85.   ;; If user specified a file name or if this buffer knows which one to use,
  86.   ;; just use that.
  87.   (or file-name
  88.       (setq file-name change-log-default-name)
  89.       (progn
  90.     ;; Chase links in the source file
  91.     ;; and use the change log in the dir where it points.
  92.     (setq file-name (or (and buffer-file-name
  93.                  (file-name-directory
  94.                   (file-chase-links buffer-file-name)))
  95.                 default-directory))
  96.     (if (file-directory-p file-name)
  97.         (setq file-name (expand-file-name (change-log-name) file-name)))
  98.     ;; Chase links before visiting the file.
  99.     ;; This makes it easier to use a single change log file
  100.     ;; for several related directories.
  101.     (setq file-name (file-chase-links file-name))
  102.     (setq file-name (expand-file-name file-name))
  103.     ;; Move up in the dir hierarchy till we find a change log file.
  104.     (let ((file1 file-name)
  105.           parent-dir)
  106.       (while (and (not (or (get-file-buffer file1) (file-exists-p file1)))
  107.               (progn (setq parent-dir
  108.                    (file-name-directory
  109.                     (directory-file-name
  110.                      (file-name-directory file1))))
  111.                  ;; Give up if we are already at the root dir.
  112.                  (not (string= (file-name-directory file1)
  113.                        parent-dir))))
  114.         ;; Move up to the parent dir and try again.
  115.         (setq file1 (expand-file-name 
  116.              (file-name-nondirectory (change-log-name))
  117.              parent-dir)))
  118.       ;; If we found a change log in a parent, use that.
  119.       (if (or (get-file-buffer file1) (file-exists-p file1))
  120.           (setq file-name file1)))))
  121.   ;; Make a local variable in this buffer so we needn't search again.
  122.   (set (make-local-variable 'change-log-default-name) file-name)
  123.   file-name)
  124.  
  125. ;;;###autoload
  126. (defun add-change-log-entry (&optional whoami file-name other-window new-entry)
  127.   "Find change log file and add an entry for today.
  128. Optional arg (interactive prefix) non-nil means prompt for user name and site.
  129. Second arg is file name of change log.  If nil, uses `change-log-default-name'.
  130. Third arg OTHER-WINDOW non-nil means visit in other window.
  131. Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
  132. never append to an existing entry."
  133.   (interactive (list current-prefix-arg
  134.              (prompt-for-change-log-name)))
  135.   (if whoami
  136.       (progn
  137.         (setq add-log-full-name (read-string "Full name: " add-log-full-name))
  138.     ;; Note that some sites have room and phone number fields in
  139.     ;; full name which look silly when inserted.  Rather than do
  140.     ;; anything about that here, let user give prefix argument so that
  141.     ;; s/he can edit the full name field in prompter if s/he wants.
  142.     (setq add-log-mailing-address
  143.           (read-string "Mailing address: " add-log-mailing-address))))
  144.   (let ((defun (funcall (or add-log-current-defun-function
  145.                 'add-log-current-defun)))
  146.     paragraph-end entry)
  147.  
  148.     (setq file-name (expand-file-name (find-change-log file-name)))
  149.  
  150.     ;; Set ENTRY to the file name to use in the new entry.
  151.     (and buffer-file-name
  152.      ;; Never want to add a change log entry for the ChangeLog file itself.
  153.      (not (string= buffer-file-name file-name))
  154.      (setq entry (if (string-match
  155.               (concat "^" (regexp-quote (file-name-directory
  156.                              file-name)))
  157.               buffer-file-name)
  158.              (substring buffer-file-name (match-end 0))
  159.                (file-name-nondirectory buffer-file-name))))
  160.  
  161.     (if (and other-window (not (equal file-name buffer-file-name)))
  162.     (find-file-other-window file-name)
  163.       (find-file file-name))
  164.     (undo-boundary)
  165.     (goto-char (point-min))
  166.     (if (looking-at (concat (regexp-quote (substring (current-time-string)
  167.                              0 10))
  168.                 ".* " (regexp-quote add-log-full-name)
  169.                 "  <" (regexp-quote add-log-mailing-address)))
  170.     (forward-line 1)
  171.       (insert (current-time-string)
  172.           "  " add-log-full-name
  173.           "  <" add-log-mailing-address ">\n\n"))
  174.  
  175.     ;; Search only within the first paragraph.
  176.     (if (looking-at "\n*[^\n* \t]")
  177.     (skip-chars-forward "\n")
  178.       (forward-paragraph 1))
  179.     (setq paragraph-end (point))
  180.     (goto-char (point-min))
  181.  
  182.     ;; Now insert the new line for this entry.
  183.     (cond ((re-search-forward "^\\s *\\*\\s *$" paragraph-end t)
  184.        ;; Put this file name into the existing empty entry.
  185.        (if entry
  186.            (insert entry)))
  187.       ((and (not new-entry)
  188.                 (re-search-forward
  189.          (concat (regexp-quote (concat "* " entry))
  190.              ;; Don't accept `foo.bar' when
  191.              ;; looking for `foo':
  192.              "\\(\\s \\|[(),:]\\)")
  193.          paragraph-end t))
  194.        ;; Add to the existing entry for the same file.
  195.        (re-search-forward "^\\s *$\\|^\\s \\*")
  196.        (beginning-of-line)
  197.        (while (and (not (eobp)) (looking-at "^\\s *$"))
  198.          (delete-region (point) (save-excursion (forward-line 1) (point))))
  199.        (insert "\n\n")
  200.        (forward-line -2)
  201.        (indent-relative-maybe))
  202.       (t
  203.        ;; Make a new entry.
  204.        (forward-line 1)
  205.        (while (looking-at "\\sW")
  206.          (forward-line 1))
  207.        (while (and (not (eobp)) (looking-at "^\\s *$"))
  208.          (delete-region (point) (save-excursion (forward-line 1) (point))))
  209.        (insert "\n\n\n")
  210.        (forward-line -2)
  211.        (indent-to left-margin)
  212.        (insert "* " (or entry ""))))
  213.     ;; Now insert the function name, if we have one.
  214.     ;; Point is at the entry for this file,
  215.     ;; either at the end of the line or at the first blank line.
  216.     (if defun
  217.     (progn
  218.       ;; Make it easy to get rid of the function name.
  219.       (undo-boundary)
  220.       (insert (if (save-excursion
  221.             (beginning-of-line 1)
  222.             (looking-at "\\s *$")) 
  223.               ""
  224.             " ")
  225.           "(" defun "): "))
  226.       ;; No function name, so put in a colon unless we have just a star.
  227.       (if (not (save-excursion
  228.          (beginning-of-line 1)
  229.          (looking-at "\\s *\\(\\*\\s *\\)?$")))
  230.       (insert ": ")))))
  231.  
  232. ;;;###autoload
  233. (defun add-change-log-entry-other-window (&optional whoami file-name)
  234.   "Find change log file in other window and add an entry for today.
  235. Optional arg (interactive prefix) non-nil means prompt for user name and site.
  236. Second arg is file name of change log.  \
  237. If nil, uses `change-log-default-name'."
  238.   (interactive (if current-prefix-arg
  239.            (list current-prefix-arg
  240.              (prompt-for-change-log-name))))
  241.   (add-change-log-entry whoami file-name t))
  242. ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
  243.  
  244. (defvar change-log-mode-map nil
  245.   "Keymap for Change Log major mode.")
  246. (if change-log-mode-map
  247.     nil
  248.   (setq change-log-mode-map (make-sparse-keymap))
  249.   (set-keymap-name change-log-mode-map 'change-log-mode-map)
  250.   (define-key change-log-mode-map "\M-q" 'change-log-fill-paragraph))
  251.  
  252. ;;;###autoload
  253. (defun change-log-mode ()
  254.   "Major mode for editing change logs; like Indented Text Mode.
  255. Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
  256. New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
  257. Each entry behaves as a paragraph, and the entries for one day as a page.
  258. Runs `change-log-mode-hook'."
  259.   (interactive)
  260.   (kill-all-local-variables)
  261.   (indented-text-mode)
  262.   (setq major-mode 'change-log-mode
  263.     mode-name "Change Log"
  264.     left-margin 8
  265.     fill-column 74)
  266.   (use-local-map change-log-mode-map)
  267.   ;; Let each entry behave as one paragraph:
  268.   (set (make-local-variable 'paragraph-start) "^\\s *$\\|^\f")
  269.   (set (make-local-variable 'paragraph-separate) "^\\s *$\\|^\f\\|^\\sw")
  270.   ;; Let all entries for one day behave as one page.
  271.   ;; Match null string on the date-line so that the date-line
  272.   ;; is grouped with what follows.
  273.   (set (make-local-variable 'page-delimiter) "^\\<\\|^\f")
  274.   (set (make-local-variable 'version-control) 'never)
  275.   (set (make-local-variable 'adaptive-fill-regexp) "\\s *")
  276.   (run-hooks 'change-log-mode-hook))
  277.  
  278. ;; It might be nice to have a general feature to replace this.  The idea I
  279. ;; have is a variable giving a regexp matching text which should not be
  280. ;; moved from bol by filling.  change-log-mode would set this to "^\\s *\\s(".
  281. ;; But I don't feel up to implementing that today.
  282. (defun change-log-fill-paragraph (&optional justify)
  283.   "Fill the paragraph, but preserve open parentheses at beginning of lines.
  284. Prefix arg means justify as well."
  285.   (interactive "P")
  286.   (let ((paragraph-separate (concat paragraph-separate "\\|^\\s *\\s("))
  287.     (paragraph-start (concat paragraph-start "\\|^\\s *\\s(")))
  288.     (fill-paragraph justify)))
  289.  
  290. (defvar add-log-current-defun-header-regexp
  291.   "^\\([A-Z][A-Z_ ]*[A-Z_]\\|[-_a-zA-Z]+\\)[ \t]*[:=]"
  292.   "*Heuristic regexp used by `add-log-current-defun' for unknown major modes.")
  293.  
  294. ;;;###autoload
  295. (defun add-log-current-defun ()
  296.   "Return name of function definition point is in, or nil.
  297.  
  298. Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
  299. Texinfo (@node titles), and Fortran.
  300.  
  301. Other modes are handled by a heuristic that looks in the 10K before
  302. point for uppercase headings starting in the first column or
  303. identifiers followed by `:' or `=', see variable
  304. `add-log-current-defun-header-regexp'.
  305.  
  306. Has a preference of looking backwards."
  307.   (condition-case nil
  308.       (save-excursion
  309.     (let ((location (point)))
  310.       (cond ((memq major-mode '(emacs-lisp-mode lisp-mode scheme-mode))
  311.          ;; If we are now precisely a the beginning of a defun,
  312.          ;; make sure beginning-of-defun finds that one
  313.          ;; rather than the previous one.
  314.          (or (eobp) (forward-char 1))
  315.          (beginning-of-defun)
  316.          ;; Make sure we are really inside the defun found, not after it.
  317.          (if (and (progn (end-of-defun)
  318.                  (< location (point)))
  319.               (progn (forward-sexp -1)
  320.                  (>= location (point))))
  321.              (progn
  322.                (if (looking-at "\\s(")
  323.                (forward-char 1))
  324.                (forward-sexp 1)
  325.                (skip-chars-forward " ")
  326.                (buffer-substring (point)
  327.                      (progn (forward-sexp 1) (point))))))
  328.         ((and (memq major-mode '(c-mode c++-mode c++-c-mode))
  329.               (save-excursion (beginning-of-line)
  330.                       ;; Use eq instead of = here to avoid
  331.                       ;; error when at bob and char-after
  332.                       ;; returns nil.
  333.                       (while (eq (char-after (- (point) 2)) ?\\)
  334.                     (forward-line -1))
  335.                       (looking-at "[ \t]*#[ \t]*define[ \t]")))
  336.          ;; Handle a C macro definition.
  337.          (beginning-of-line)
  338.          (while (eq (char-after (- (point) 2)) ?\\) ;not =; note above
  339.            (forward-line -1))
  340.          (search-forward "define")
  341.          (skip-chars-forward " \t")
  342.          (buffer-substring (point)
  343.                    (progn (forward-sexp 1) (point))))
  344.         ((memq major-mode '(c-mode c++-mode c++-c-mode))
  345.          (beginning-of-line)
  346.          ;; See if we are in the beginning part of a function,
  347.          ;; before the open brace.  If so, advance forward.
  348.          (while (not (looking-at "{\\|\\(\\s *$\\)"))
  349.            (forward-line 1))
  350.          (or (eobp)
  351.              (forward-char 1))
  352.          (beginning-of-defun)
  353.          (if (progn (end-of-defun)
  354.                 (< location (point)))
  355.              (progn
  356.                (backward-sexp 1)
  357.                (let (beg tem)
  358.  
  359.              (forward-line -1)
  360.              ;; Skip back over typedefs of arglist.
  361.              (while (and (not (bobp))
  362.                      (looking-at "[ \t\n]"))
  363.                (forward-line -1))
  364.              ;; See if this is using the DEFUN macro used in Emacs,
  365.              ;; or the DEFUN macro used by the C library.
  366.              (if (condition-case nil
  367.                  (and (save-excursion
  368.                     (end-of-line)
  369.                     (while (= (preceding-char) ?\\)
  370.                       (end-of-line 2))
  371.                     (backward-sexp 1)
  372.                     (beginning-of-line)
  373.                     (setq tem (point))
  374.                     (looking-at "DEFUN\\b"))
  375.                       (>= location tem))
  376.                    (error nil))
  377.                  (progn
  378.                    (goto-char tem)
  379.                    (down-list 1)
  380.                    (if (= (char-after (point)) ?\")
  381.                    (progn
  382.                      (forward-sexp 1)
  383.                      (skip-chars-forward " ,")))
  384.                    (buffer-substring (point)
  385.                          (progn (forward-sexp 1) (point))))
  386.                ;; Ordinary C function syntax.
  387.                (setq beg (point))
  388.                (if (condition-case nil
  389.                    ;; Protect against "Unbalanced parens" error.
  390.                    (progn
  391.                      (down-list 1) ; into arglist
  392.                      (backward-up-list 1)
  393.                      (skip-chars-backward " \t")
  394.                      t)
  395.                  (error nil))
  396.                    ;; Verify initial pos was after
  397.                    ;; real start of function.
  398.                    (if (and (save-excursion
  399.                       (goto-char beg)
  400.                       ;; For this purpose, include the line
  401.                       ;; that has the decl keywords.  This
  402.                       ;; may also include some of the
  403.                       ;; comments before the function.
  404.                       (while (and (not (bobp))
  405.                               (save-excursion
  406.                             (forward-line -1)
  407.                             (looking-at "[^\n\f]")))
  408.                         (forward-line -1))
  409.                       (>= location (point)))
  410.                     ;; Consistency check: going down and up
  411.                     ;; shouldn't take us back before BEG.
  412.                     (> (point) beg))
  413.                    (buffer-substring (point)
  414.                              (progn (backward-sexp 1)
  415.                                 (point))))))))))
  416.         ((memq major-mode
  417.                '(TeX-mode plain-TeX-mode LaTeX-mode;; tex-mode.el
  418.                   plain-tex-mode latex-mode;; cmutex.el
  419.                   ))
  420.          (if (re-search-backward
  421.               "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t)
  422.              (progn
  423.                (goto-char (match-beginning 0))
  424.                (buffer-substring (1+ (point));; without initial backslash
  425.                      (progn
  426.                        (end-of-line)
  427.                        (point))))))
  428.         ((eq major-mode 'texinfo-mode)
  429.          (if (re-search-backward "^@node[ \t]+\\([^,]+\\)," nil t)
  430.              (buffer-substring (match-beginning 1)
  431.                        (match-end 1))))
  432.                 ((eq major-mode 'fortran-mode)
  433.                  ;; must be inside function body for this to work
  434.                  (beginning-of-fortran-subprogram)
  435.                  (let ((case-fold-search t)) ; case-insensitive
  436.                    ;; search for fortran subprogram start
  437.                    (if (re-search-forward
  438.              "^[ \t]*\\(program\\|subroutine\\|function\
  439. \\|[ \ta-z0-9*]*[ \t]+function\\)"
  440.              nil t)
  441.                        (progn
  442.                          ;; move to EOL or before first left paren
  443.                          (if (re-search-forward "[(\n]" nil t)
  444.                  (progn (forward-char -1)
  445.                     (skip-chars-backward " \t"))
  446.                (end-of-line))
  447.              ;; Use the name preceding that.
  448.                          (buffer-substring (point)
  449.                                            (progn (forward-sexp -1)
  450.                                                   (point)))))))
  451.         (t
  452.          ;; If all else fails, try heuristics
  453.          (let (case-fold-search)
  454.            (end-of-line)
  455.            (if (re-search-backward add-log-current-defun-header-regexp
  456.                        (- (point) 10000)
  457.                        t)
  458.                (buffer-substring (match-beginning 1)
  459.                      (match-end 1))))))))
  460.     (error nil)))
  461.  
  462.  
  463. (provide 'add-log)
  464.  
  465. ;;; add-log.el ends here
  466.